package com.eclab.hello.controller;

import com.eclab.hello.exception.UserException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;

/**
 * 自定义异常捕获
 *
 * @author Aiden
 * @create 2019-07-03 23:46
 */
@ControllerAdvice
@Slf4j
public class UserControllerAdvice {

    @ExceptionHandler(UserException.class)
    public void runtimeException(RuntimeException e) {
        log.error("捕获到异常:{}", e.getMessage());
        log.info("DO SOMETHING...");
    }

    @InitBinder
    @ModelAttribute
    public void globalInitBinder(WebDataBinder binder) {
        binder.addCustomFormatter(new DateFormatter("yyyy-MM-dd"));
    }

    @ModelAttribute(value = "message")
    public String globalModelAttribute() {
        log.info("global model attribute.");
        return "this is from model attribute";
    }

}












